package com.wing.res.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.res.model.entity.ReceiptRecord;
import com.wing.res.service.ReceiptRecordOtherService;
import com.wing.res.service.ReceiptRecordService;
import com.wing.web.db.config.util.DynamicDataSource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 其他领料记录-controller
 *
 *
 */
@Validated
@Api(value = "其他领料记录管理", tags = "#/res/receiptRecord/receiptRecordOther")
@RestController("ReceiptRecordOtherController")
@RequestMapping("receiptRecord/admin/receiptRecordOther")
@RequiredArgsConstructor
public class ReceiptRecordOtherController {

    private final ReceiptRecordOtherService receiptRecordService;

    @ApiOperation(value = "查询其他领料记录列表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ReceiptRecord> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            String workOrderCode
            ) {
        Page<ReceiptRecord> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("code", workOrderCode);
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        params.put("databaseUser", databaseUser);
        params.put("operateType", 1); //0 领料出库 1其他出库
        IPage<ReceiptRecord> list = receiptRecordService.list(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加其他领料记录", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(@RequestBody ReceiptRecord receiptRecord) {
        try {

            receiptRecordService.add(receiptRecord);
        } catch (BusinessException e) {
            return JsonResult.fail(e.getMessage());
        }
        return JsonResult.success("添加成功");
    }

    @ApiOperation(value = "更新其他领料记录", notes = "更新")
    @PostMapping("/update")
    public JsonResult upadte(ReceiptRecord receiptRecord) {
        receiptRecordService.update(receiptRecord);
        return JsonResult.success("更新成功");
    }

    @ApiOperation(value = "其他领料记录详情", notes = "详情")
    @GetMapping("/get")
    public JsonResult get(@RequestParam(name = "id") Long id) {
        Map<String, Object> params = new HashMap<String, Object>();
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        params.put("databaseUser", databaseUser);
        params.put("id", id);
        return JsonResult.success(receiptRecordService.get(params));
    }

}
